package edu.service;

import java.util.List;

import org.apache.commons.collections.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.core.StringUtil;
import edu.dao.BaseDao;
import edu.entity.Module;
import edu.entity.ModuleData;

@Service("moduleService")
public class ModuleService extends BaseService {
	@Autowired
	private BaseDao baseDao;
	
    public void insertModule(Module module) throws Exception {
    	if(module!=null){
    		baseDao.insert(module);
    	}
    }
    
    public void updatetModule(Module module) throws Exception {
    	if(module!=null){
    		baseDao.update(module);
    	}
    }
    
    public void deleteModule(Module module) throws Exception {
    	if(module!=null){
	    	String hql = "from ModuleData md where md.module.id ="+module.getId();
	    	
	    	List<ModuleData> moduleDataList = baseDao.selectPojoByHql(hql);
	    	
	    	if (moduleDataList!=null && !moduleDataList.isEmpty()) {
	    		for (ModuleData moduleData : moduleDataList) {
		    		String attHql = "delete Attachment a where a.moduleData.id="+moduleData.getId();
		    		baseDao.batchDeleteByHql(attHql);
		    		baseDao.delete(moduleData);
				}
			}
	    	
	    	baseDao.delete(module);
    	}
    }
    
    public void deleteModuleById(String id) throws Exception {
    	if(StringUtil.isNotEmpty(id)){
    		Module module = (Module)baseDao.selectObjById(Module.class, id);
        	deleteModule(module);
    	}
    }
    
    public Module selectModuleById(String id) throws Exception {
    	Module module = null;
    	if(StringUtil.isNotEmpty(id)){
    		module = (Module)baseDao.selectObjById(Module.class, id);
    	}
    	return module;
    }
}
